﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

    public partial class ShopLocation : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                //string strAction = "";
                if (Request.QueryString["action"] != null)
                {
                    if (Request.QueryString["action"].ToString().ToLower() == "add")
                    {
                        //strAction = " - เพิ่มข้อมูล";
                        Session["ACTION"] = "add";
                    }
                    else if (Request.QueryString["action"].ToString().ToLower() == "edit")
                    {
                        //strAction = " - แก้ไขข้อมูล";
                        Session["ACTION"] = "edit";
                        btnClear.Visible = false;
                    }
                    else if (Request.QueryString["action"].ToString().ToLower() == "view")
                    {   
                        //strAction = " - เรียกดูข้อมูล";
                        Session["ACTION"] = "view";
                        btnClear.Visible = false;
                        btnSave.Visible = false;
                    }
                }

                if (!IsPostBack)
                {
                    BindData();
                    hdnShopID.Value = Request.QueryString["ShopID"];
                }
            }
            catch (Exception ex)
            {
                lblMessage.Text = "<br>" + ex.Message + "<br><br>";
            }       
        }

        private void BindData()
        {
            if (Request.QueryString["result"] != null && Request.QueryString["result"].ToString() == "1")
            {
                if (Session["ACTION"].ToString() == "edit")
                    lblMessage.Text = "แก้ไขข้อมูลผู้ใช้งานเรียบร้อยแล้ว";
                else
                    lblMessage.Text = "เพิ่มข้อมูลผู้ใช้งานเรียบร้อยแล้ว";
            }
            if (Session["ACTION"].ToString() == "edit" || Session["ACTION"].ToString() == "view")
            {
                DataTable dt = new DataTable();
                DBConn oDBConn = new DBConn();
                oDBConn.OpenConnection();

                SqlCommand oCommand = new SqlCommand("sp_Deal_Location_SELECTBYLOCATIONID", oDBConn.DBConnection);
                oCommand.CommandType = CommandType.StoredProcedure;
                oCommand.Parameters.Add("@Location_ID", SqlDbType.BigInt).Value = Request.QueryString["LocationID"];

                SqlDataAdapter oDataAdapter = new SqlDataAdapter(oCommand);
                oDataAdapter.Fill(dt);
                oDataAdapter = null;
                oDBConn.Disconnect();

                if (dt.Rows.Count > 0)
                {
                    if (Session["ACTION"].ToString() == "edit")
                    {
                        txtLocationName.Text = dt.Rows[0]["Location_Name"].ToString();
                        txtLocationDetail.Text = dt.Rows[0]["Location_Detail"].ToString();
                        txtLatitude.Text = dt.Rows[0]["Location_Lat"].ToString();
                        txtLongitude.Text = dt.Rows[0]["Location_Long"].ToString();
                    }
                    else
                    {
                        txtLocationName.Visible = false;
                        txtLocationDetail.Visible = false;
                        txtLatitude.Visible = false;
                        txtLongitude.Visible = false;

                        lblLocationName.Text = dt.Rows[0]["Location_Name"].ToString();
                        lblLocationDetail.Text = dt.Rows[0]["Location_Detail"].ToString();
                        lblLatitude.Text = dt.Rows[0]["Location_Lat"].ToString();
                        lblLongitude.Text = dt.Rows[0]["Location_Long"].ToString();
                    }
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = new DataTable();
                DBConn oDBConn = new DBConn();
                oDBConn.OpenConnection();

                if (Session["ACTION"].ToString() == "add")
                {
                    SqlCommand oCommand = new SqlCommand("sp_Deal_Location_INSERT", oDBConn.DBConnection);
                    oCommand.CommandType = CommandType.StoredProcedure;
                    oCommand.Parameters.Add("@Location_Name", SqlDbType.VarChar).Value = txtLocationName.Text.Trim();
                    oCommand.Parameters.Add("@Location_Detail", SqlDbType.VarChar).Value = txtLocationDetail.Text.Trim();
                    oCommand.Parameters.Add("@Location_Lat", SqlDbType.Float).Value = txtLatitude.Text.Trim();
                    oCommand.Parameters.Add("@Location_Long", SqlDbType.Float).Value = txtLongitude.Text.Trim();
                    oCommand.Parameters.Add("@Location_Shop_ID", SqlDbType.BigInt).Value = Request.QueryString["ShopID"];

                    oCommand.ExecuteNonQuery();
                    oDBConn.Disconnect();

                    Response.Redirect("ShopLocation.aspx?ShopID=" + hdnShopID.Value + "&action=add&result=1");
                }
                else
                {
                    //Edit
                    string LocationID = Request.QueryString["LocationID"];
                    SqlCommand oCommand = new SqlCommand("sp_Deal_Location_UPDATE", oDBConn.DBConnection);
                    oCommand.CommandType = CommandType.StoredProcedure;
                    oCommand.Parameters.Add("@Location_ID", SqlDbType.BigInt).Value = LocationID;
                    oCommand.Parameters.Add("@Location_Name", SqlDbType.VarChar).Value = txtLocationName.Text.Trim();
                    oCommand.Parameters.Add("@Location_Detail", SqlDbType.VarChar).Value = txtLocationDetail.Text.Trim();
                    oCommand.Parameters.Add("@Location_Lat", SqlDbType.Float).Value = txtLatitude.Text.Trim();
                    oCommand.Parameters.Add("@Location_Long", SqlDbType.Float).Value = txtLongitude.Text.Trim();
                    oCommand.Parameters.Add("@Location_Shop_ID", SqlDbType.BigInt).Value = Request.QueryString["ShopID"];

                    oCommand.ExecuteNonQuery();
                    oDBConn.Disconnect();

                    Response.Redirect("ShopLocation.aspx?ShopID=" + hdnShopID.Value + "&action=edit&LocationID=" + LocationID + "&result=1");
                }
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }
        }
        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("Shop.aspx");
        }
        protected void btnClear_Click(object sender, EventArgs e)
        {
            txtLocationName.Text = "";
            txtLocationDetail.Text = "";
            txtLatitude.Text = "";
            txtLongitude.Text = "";
        }
}
